package dao

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"taofoo.top/learn/common"
)

var DB *gorm.DB

func Connect() {
	dsn := "test:123@tcp(127.0.0.1:3306)/stuinfo?charset=utf8mb4&parseTime=True&loc=Local"
	if db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}); err != nil {
		log.Fatal(err)
	} else {
		DB = db
	}
}

type Stu struct {
	Sno  int
	Name string
	Id string
}


func (Stu) TableName() string {
	return "stu"
}

func List(info common.PageInfo) interface{} {
	var stus []Stu
	limit := 10
	offset := 10
	if info.PageSize != 0 {
		limit = info.PageSize

		if info.PageNum != 0 {
			offset = (info.PageNum - 1) * limit
		}

	}
	DB.Model(&Stu{}).Limit(limit).Offset(offset).Find(&stus)
	return stus
}
